package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.grandchild.GrandChildTalentInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildTalentInfoDaoImpl extends TempDao {
    public Map<Integer, GrandChildTalentInfo> getGrandChildTalentInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, GrandChildTalentInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_grand_child_talent";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                GrandChildTalentInfo data = new GrandChildTalentInfo();
                data.setId(rs.getInt("id"));
                data.setCondition(rs.getString("condition"));
                String excludeStr = rs.getString("exclude");
                if ("0".equals(excludeStr)) {
                    data.setExcludeList(new ArrayList<>());
                }else {
                    List<Integer> list = new ArrayList<>();
                    String[] split = excludeStr.split(";");
                    for (String s : split) {
                        list.add(Integer.parseInt(s));
                    }
                    data.setExcludeList(list);
                }
                data.setSkills(StringUtils.stringToIntegerList(rs.getString("skills"),";"));
                data.setWeight(rs.getInt("weight"));
                data.setTriggerCondition(rs.getString("triggerCondition"));
                data.setTriggerOrder(rs.getInt("triggerOrder"));
                data.setQuality(rs.getInt("quality"));
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

}
